package cn.big.bigd.biocode.model;

import cn.big.bigd.biocode.convert.CountryConverter;
import cn.big.bigd.biocode.convert.JpaConverterJson;
import cn.big.bigd.biocode.model.vocabulary.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Where;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by wangfan on 2017/11/28.
 */
@Entity
@Getter
@Setter
@ToString
@Table(name="tools")
public class Tool {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    @Column(name = "`desc`")
    private String desc;
    private String introduction;
    private String manual;
    private String website;
    private String fundings;
    @Convert(converter = JpaConverterJson.class)
    private List<Publication> publications;


    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name="tools_publications",
            joinColumns=
            @JoinColumn(name="tool_id", referencedColumnName="id",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT)),
            inverseJoinColumns=
            @JoinColumn(name="publication_id", referencedColumnName="id",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT))
    )
    private List<Publications> publicationList;


//    private Integer toolTypeId;
//    private Integer userId;
    private Date createdAt;


    @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern="yyyy-MM-dd")
//    @Temporal(TemporalType.DATE)
    private Date updatedAt;

    @Convert(converter = CountryConverter.class)
    private String country;
    @Column(columnDefinition="TINYINT")
    private Boolean curated;
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name="toolTypeId",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT))
    private ToolType toolType;
    @JsonIgnore
    @OneToMany(mappedBy = "tool",fetch = FetchType.LAZY,cascade = CascadeType.MERGE,targetEntity = Credit.class)
    private List<Credit> credits;
    @JsonIgnore
    @OneToMany(mappedBy = "tool",fetch = FetchType.LAZY,targetEntity = Release.class)
    @org.hibernate.annotations.OrderBy(clause = "created_at desc")
    private List<Release> releases;
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name="userId",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT))
    private User user;
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST,targetEntity = Tech.class)
    @JoinTable(name="tools_vocabularies",
            joinColumns=
            @JoinColumn(name="tool_id", referencedColumnName="id"),
            inverseJoinColumns=
            @JoinColumn(name="vocabulary_id", referencedColumnName="id")
    )
    @Where(clause = "type = 'Tech'")
    private List<Tech> teches;
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST,targetEntity = DataType.class)
    @JoinTable(name="tools_vocabularies",
            joinColumns=
            @JoinColumn(name="tool_id", referencedColumnName="id"),
            inverseJoinColumns=
            @JoinColumn(name="vocabulary_id", referencedColumnName="id")
    )
    @Where(clause = "type = 'DataType'")
    private List<DataType> dataTypes;
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST,targetEntity = Category.class)
    @JoinTable(name="categories_tools",
            joinColumns=
            @JoinColumn(name="tool_id", referencedColumnName="id",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT)),
            inverseJoinColumns=
            @JoinColumn(name="category_id", referencedColumnName="id",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT))
    )
    private List<Category> categories;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST,targetEntity = Platform.class)
    @JoinTable(name="tools_vocabularies",
            joinColumns=
            @JoinColumn(name="tool_id", referencedColumnName="id"),
            inverseJoinColumns=
            @JoinColumn(name="vocabulary_id", referencedColumnName="id")
    )
    @Where(clause = "type = 'Platform'")
    private List<Platform> platforms;


    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST,targetEntity = UserInterface.class)
    @JoinTable(name="tools_vocabularies",
            joinColumns=
            @JoinColumn(name="tool_id", referencedColumnName="id",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT)),
            inverseJoinColumns=
            @JoinColumn(name="vocabulary_id", referencedColumnName="id",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT))
    )
    @Where(clause = "type = 'UserInterface'")
    private List<UserInterface> userInterfaces;


    private Integer citation;



}
